package com.example.demo.controller;

import com.example.demo.domain.Test;
import com.example.demo.service.TestService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
public class TestController {
    private static final Logger LOG = LoggerFactory.getLogger(TestController.class);

    @Value("${test.hello:TEST}")
    private String testHello;

    @Resource
    private TestService testService;

    @Resource
    private RedisTemplate redisTemplate;
    /*
    优先读配置文件，灭有就用默认值
     * 三个SpringBoot不会都bootstrap配置，
     * 只有在SpringCloud构架下的SpringBoot应用才会
     * bootstrap一般用于动态配置，
     * 线上可以实时修改实时生效的配置，
     * 一般可配合上nacos使用
     * */
        /**
         * ResponseBody用来返回字符串或JSON对象
         *RequestMapping
         * GET,POST,PUT,DELETE
         *
         * /user?id=1
         * /user/1
         * @return
         * */
        @GetMapping("/hello")
        public String hello(){
            return "Hello World!"+testHello;
        }

        @PostMapping("/hello/post")
        public String helloPost(String name){
            return "Hello World! Post :"+name;
        }

        @GetMapping("/test/list")
        public List<Test> list(){
            return testService.list();
        }

        @RequestMapping("/redis/set/{key}/{value}")
        public  String set(@PathVariable Long key,@PathVariable String  value){

            redisTemplate.opsForValue().set(key,value, 3600,TimeUnit.SECONDS);
            LOG.info("key:{},value:{}",key,value);
            return "success";
        }

       @RequestMapping("/redis/get/{key}")
        public  Object get(@PathVariable Long key){

           Object object=redisTemplate.opsForValue().get(key);
            LOG.info("key:{},value:{}",key,object);
             return object;
    }


}


